//
//  CardViewController.m
//  SegmentDemo
//
//  Created by 塔兮 on 2022/4/24.
//

#import "CardViewController.h"

@interface CardViewController ()

@property (nonatomic, strong) ProgressView *progressView;
@property (nonatomic, strong) UIButton *button;

@end

@implementation CardViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self configureView];
}

- (ProgressView *)progressView {
    if (!_progressView) {
        _progressView = [[ProgressView alloc]initWithFrame:self.view.bounds];
        _progressView.title = @"上传中...";
        _progressView.backgroundColor = RGBAColor(44, 44, 44, 0.2);
//        _progressView.hidden = YES;
    }
    return _progressView;
}

- (UIButton *)button {
    if (!_button) {
        _button = [UIButton buttonWithType:UIButtonTypeSystem];
        _button.backgroundColor = UIColorFromHex(@"FFFFFF");
        _button.titleLabel.font = FONT_SIZE(14);
        [_button setTitle:self.title forState:UIControlStateNormal];
        [_button setTitleColor:UIColorFromHex(@"000000") forState:UIControlStateNormal];
        [_button setBorderWidth:1 color:UIColorFromHex(@"F5F5F5") radius:5];
        [_button addTarget:self action:@selector(buttonAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _button;
}

- (void)configureView {
    self.title = @"卡片";
    self.view.backgroundColor = UIColor.blackColor;
    [self.view addSubview:self.progressView];
    [self.view addSubview:self.button];
    
    [self.button mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.mas_equalTo(self.view.mas_centerX);
        make.top.mas_equalTo(10);
        make.height.mas_offset(30);
        make.width.mas_offset(100);
    }];
}

- (void)buttonAction {
    self.progressView.hidden = NO;
    
    [self countJumpAction];
}

- (void)countJumpAction{
    __block int _numText = 0;
    //全局队列    默认优先级
    dispatch_queue_t quene = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    //定时器模式  事件源
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, quene);
    //NSEC_PER_SEC是秒，＊1是每秒
    dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), NSEC_PER_SEC * 0.01, 0);
    //设置响应dispatch源事件的block，在dispatch源指定的队列上运行
    dispatch_source_set_event_handler(timer, ^{
        //回调主线程，在主线程中操作UI
        dispatch_async(dispatch_get_main_queue(), ^{
            if (_numText <= 999) {
                NSLog(@"xxxxxxx");
                self.progressView.progress = (float)_numText/1000;
                _numText++;
            } else {
                //这句话必须写否则会出问题
                dispatch_source_cancel(timer);
                self.progressView.hidden = YES;
            }
        });
    });
    //启动源
    dispatch_resume(timer);

}

@end
